import numpy as np
import cv2


def mp42imgs(mp4_path):
    imgs = []

    cap = cv2.VideoCapture(mp4_path)
    fourcc = cv2.VideoWriter_fourcc(*"mp4v")
    cap.set(cv2.CAP_PROP_FOURCC, fourcc)
    suc = cap.isOpened()
    while True:
        suc, img = cap.read()
        if not suc:
            break
        imgs.append(img[:, :, ::-1].astype(np.uint8))  # bgr2rgb
    
    return imgs


def avi2imgs(avi_path):
    imgs = []

    cap = cv2.VideoCapture(avi_path)
    fourcc = cv2.VideoWriter_fourcc('I','4', '2', '0')
    cap.set(cv2.CAP_PROP_FOURCC, fourcc)
    suc = cap.isOpened()
    while True:
        suc, img = cap.read()
        if not suc:
            break
        imgs.append(img[:, :, ::-1].astype(np.uint8))  # bgr2rgb
    
    return imgs


if __name__ == "__main__":
    imgs = avi2imgs("/home/liuyun/HHO/multi-camera/data/compress_depth_test/ex1.avi")
    print(len(imgs), imgs[0].shape, imgs[0].dtype, imgs[0][500:510, 500:510])
